#include "key.h"
#include "start.h"
#include "rtc.h"

u8 LCD_state = LCD_state_showing;	//初始化状态
u32 TimingDelay = 0;
DataSetDef data_set;
static u8 flag = 0;//按键标志位
//按键初始化
void key_Init(void){
	GPIO_InitTypeDef GPIO_InitStruct;
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB, ENABLE);
	
	GPIO_InitStruct.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_8;
	GPIO_InitStruct.GPIO_Mode = GPIO_Mode_IN_FLOATING;
	GPIO_Init(GPIOA, &GPIO_InitStruct);
	
	GPIO_InitStruct.GPIO_Pin = GPIO_Pin_1 | GPIO_Pin_2;
	GPIO_InitStruct.GPIO_Mode = GPIO_Mode_IN_FLOATING;
	GPIO_Init(GPIOB, &GPIO_InitStruct);
	
	//初始化设定参数
	data_set.catch_off = 1;
	data_set.temper_limit = 20;
	data_set.wet_limit = 10;
	data_set.test_signal = 1;
}
//按键扫描
u8 key_scan(void){
	u8 RB1 = PAin0();u8 RB2 = PAin8();u8 RB3 = PBin1();u8 RB4 = PBin2();
	if((flag==0)&&((RB1&RB2&RB3&RB4)==0))
	{
//		delay_ms(10);
			flag = 1;
		if(PAin0()==0){
			return 1;
		}
		if(PAin8()==0){
			return 2;
		}
		if(PBin1()==0){
			return 3;
		}
		if(PBin2()==0){
			return 4;
		}
	}
	else if((RB1&RB2&RB3&RB4)==1){
		flag = 0;
		return 0;
	}
}
//状态切换
void state_switch(void){
	u8 key = key_scan();
	u8 i = 1;
	switch(LCD_state){
		case LCD_state_showing:{
			if(key == 1)
				LCD_state = LCD_state_setting_temperUp;
		}break;
		case LCD_state_setting_temperUp:{
			switch(key){
				case 1:{
					LCD_state = LCD_state_showing;
					write_data(1,data_set.temper_limit);
					delay_ms(5);
					write_data(2,data_set.wet_limit);
					delay_ms(5);
					TIM_Init_again();
				}break;
				case 2:{
						LCD_state = LCD_state_setting_wetUp;
				}break;
				case 3:{
					if(data_set.temper_limit<60)
							data_set.temper_limit++;
				}break;
				case 4:{
					if(data_set.temper_limit>-20)
							data_set.temper_limit--;
				}break;
			}
		}break;
		case LCD_state_setting_wetUp:{
			switch(key){
				case 1:{
					LCD_state = LCD_state_showing;
					write_data(1,data_set.temper_limit);
					delay_ms(5);
					write_data(2,data_set.wet_limit);
					delay_ms(5);
					TIM_Init_again();
				}break;
				case 2:{
					LCD_state = LCD_state_setting_off;
				}break;
				case 3:{
					if(data_set.wet_limit<85)
							data_set.wet_limit+=5;
				}break;
				case 4:{
					if(data_set.wet_limit>15)
							data_set.wet_limit-=5;
				}break;
			}
		}break;
		case LCD_state_setting_off:{
			switch(key){
				case 1:{
					LCD_state = LCD_state_showing;
					write_data(1,data_set.temper_limit);
					delay_ms(5);
					write_data(2,data_set.wet_limit);
					delay_ms(5);
					TIM_Init_again();
				}break;
				case 2:{
					LCD_state = LCD_state_setting_signal;
				}break;
				case 3:{
					if(data_set.catch_off<5)
							data_set.catch_off++;
				}break;
				case 4:{
					if(data_set.catch_off>1)
							data_set.catch_off--;
				}break;
			}
		}break;                                                                    
		case LCD_state_setting_signal:{
			switch(key){
				case 1:{
					LCD_state = LCD_state_showing;
					write_data(1,data_set.temper_limit);
					delay_ms(5);
					write_data(2,data_set.wet_limit);
					delay_ms(5);
					TIM_Init_again();
				}break;
				case 2:{
					LCD_state = LCD_state_setting_temperUp;
				}break;
				case 3:{
					if(data_set.test_signal<10)
							data_set.test_signal+=0.5;
				}break;
				case 4:{
					if(data_set.test_signal>1)
							data_set.test_signal-=0.5;
				}break;
			}
		}break;
	}
}
//PAin代表IO口输入
u8 PAin0(void){
	return GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_0);
}
u8 PAin8(void){
	return GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_8);
}
u8 PBin1(void){
	return GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_1);
}
u8 PBin2(void){
	return GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_2);
}
//向E2PROM指定地址写入数据
void write_data(unsigned char addr,unsigned char data){
	I2CStart();
	I2CSendByte(0xa0);
	I2CWaitAck();
	
	I2CSendByte(addr);
	I2CWaitAck();
	I2CSendByte(data);
	I2CWaitAck();
	I2CStop();
}
//读取E2PROM指定地址数据
unsigned char read_data(uint8_t addr){
	unsigned char val;
	
	I2CStart();
	I2CSendByte(0xa0);
	I2CWaitAck();
	I2CSendByte(addr);
	I2CWaitAck();
	
	I2CStart();
	I2CSendByte(0xa1);
	I2CWaitAck();
	val = I2CReceiveByte();
	I2CWaitAck();
	I2CStop();
	
	return val;
}
void delay_ms(u32 nTime)
{
	TimingDelay = nTime;
	while(TimingDelay != 0);	
}
